package xuhaijia.lanou3g.autohome.turnpage;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.activity.WebViewActivity;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.find.FindFragment;
import xuhaijia.lanou3g.autohome.find.TurnBean;

/**
 * Created by 7 on 16/8/19.
 */
public class TurnPageCenterAdapter extends PagerAdapter{
    Context context;
    TurnBean turnBean;
    PagerClickListener pagerClickListener;
    private int size;

    public void setPagerClickListener(PagerClickListener pagerClickListener) {
        this.pagerClickListener = pagerClickListener;
    }

    public TurnPageCenterAdapter(Context context) {
        this.context = context;
    }

    public void setTurnBean(TurnBean turnBean) {
        this.turnBean = turnBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (turnBean.getResult().getList() == null) {
            return 0;
        }
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_tp, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.item_tp_iv);
        NetTool mNetTool = new NetTool();
        size = turnBean.getResult().getList().size();
        mNetTool.getImage(turnBean.getResult().getList().get(position % size).getImgurl()
                ,imageView);
        container.addView(view);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pagerClickListener.pagerClickListener(position);
            }
        });
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    public interface PagerClickListener {
        void pagerClickListener(int position);
    }
}
